package com.event.core.util;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/*
 * @description 反射工具类
 * @author Bing
 * @date 2024/10/8 14:08
 **/
public class ReflectTools {

    public static List<Type> getGeneric(Class curClazz, Class iClazz){
        Type[] types = curClazz.getGenericInterfaces();
        if(ObjectUtil.isNull(types)){
            return new ArrayList<>(0);
        }
        List<ParameterizedType> parameterizedTypes = Arrays.stream(types).filter(item -> item instanceof ParameterizedType).map(item ->(ParameterizedType)item).collect(Collectors.toList());
        if(CollUtil.isEmpty(parameterizedTypes)){
            return new ArrayList<>(0);
        }
        Optional<ParameterizedType> optional = parameterizedTypes.stream().filter(item -> item.getRawType() == iClazz).findFirst();
        if(!optional.isPresent()){
            return new ArrayList<>(0);
        }
        Type[] actualTypeArguments = optional.get().getActualTypeArguments();
        if(ObjectUtil.isNull(actualTypeArguments)){
            return new ArrayList<>(0);
        }
        return CollUtil.toList(actualTypeArguments);
    }
}